package com.ly.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.ly.common.core.bean.R;
import com.ly.common.core.bean.dto.SmsDTO;
import com.ly.system.service.ILoginService;
import com.ly.system.system.bean.dto.LoginDTO;
import com.ly.system.system.bean.vo.IndexVisitInfoVO;
import com.ly.system.system.bean.vo.UserInfoVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户登录
 *
 * @author luoyun
 * @since 2018-12-17
 */
@Tag(name = "用户登录")
@Slf4j
@RestController
@RequestMapping("/sys")
@RequiredArgsConstructor
public class LoginController {

    private final ILoginService loginService;

    /**
     * 登录
     * @param dto
     * @return
     */
    @Operation(summary = "登录")
    @PostMapping("/login")
    public R<UserInfoVO> login(@RequestBody LoginDTO dto) {
        return loginService.login(dto);
    }

    /**
     * 登录-手机号
     *
     * @param dto
     * @return
     */
    @Operation(summary = "登录-手机号")
    @PostMapping("/phoneLogin")
    public R<UserInfoVO> phoneLogin(@RequestBody @Validated SmsDTO dto) {
        return loginService.phoneLogin(dto);
    }

    /**
     * 获取用户信息-vue3
     */
    @GetMapping("/user/getUserInfo")
    public R getUserInfo(HttpServletRequest request) {
        return loginService.getUserInfo(request);
    }

    /**
     * 退出登录
     *
     * @param request
     * @return
     */
    @GetMapping("/logout")
    public R logout(HttpServletRequest request) {
        return loginService.logout(request);
    }

    /**
     * 获取访问统计总量
     *
     * @return
     */
    @Operation(summary = "获取访问统计总量")
    @GetMapping("/loginInfo")
    public R loginInfo() {
        return loginService.loginInfo();
    }

    /**
     * 获取一周访问量信息
     *
     * @return
     */
    @Operation(summary = "获取一周访问量信息")
    @GetMapping("/visitInfo")
    public R<List<IndexVisitInfoVO>> visitInfo() {
        return loginService.visitInfo();
    }

    /**
     * 获取加密key和iv
     *
     * @return
     */
    @GetMapping("/getEncryptedString")
    public R getEncryptedString() {
        return loginService.getEncryptedString();
    }

    /**
     * 后台生成图形验证码 ：有效
     *
     * @param key
     */
    @Operation(summary = "获取验证码")
    @GetMapping("/randomImage/{key}")
    public R<String> randomImage(@PathVariable("key") String key) {
        return loginService.randomImage(key);
    }

    /**
     * 图形验证码
     *
     * @param loginDTO
     * @return
     */
    @PostMapping("/checkCaptcha")
    public R checkCaptcha(@RequestBody LoginDTO loginDTO) {
        return loginService.checkCaptcha(loginDTO);
    }

    /**
     * 登录二维码
     */
    @Operation(summary = "登录二维码")
    @GetMapping("/getLoginQrcode")
    public R getLoginQrcode() {
        return loginService.getLoginQrcode();
    }

    /**
     * 扫码二维码
     */
    @Operation(summary = "扫码登录二维码")
    @PostMapping("/scanLoginQrcode")
    public R scanLoginQrcode(@RequestParam String qrcodeId, @RequestParam String token) {
        return loginService.scanLoginQrcode(qrcodeId, token);
    }

    /**
     * 获取用户扫码后保存的token
     */
    @Operation(summary = "获取用户扫码后保存的token")
    @GetMapping("/getQrcodeToken")
    public R getQrcodeToken(@RequestParam String qrcodeId) {
        return loginService.getQrcodeToken(qrcodeId);
    }

    /**
     * 发送短信验证码接口(修改密码)
     *
     * @param jsonObject
     * @return
     */
    @PostMapping("/sendChangePwdSms")
    public R<String> sendSms(@RequestBody JSONObject jsonObject) {
        return loginService.sendSms(jsonObject);
    }

    /**
     * 图形验证码
     *
     * @param loginDTO
     * @return
     */
    @PostMapping("/smsCheckCaptcha")
    public R<String> smsCheckCaptcha(@RequestBody LoginDTO loginDTO, HttpServletRequest request) {
        return loginService.smsCheckCaptcha(loginDTO, request);
    }
}